<?php
defined('PHPFOX') or exit('NO DICE!');

class Hoidap_Service_Mypost extends Phpfox_Service 
{
	public function __construct()
	{
		$this->_sTable = Phpfox::getT('hoidap_cauhoi');
	}	
	
	public function getMyPost_bk($iduser, $idcat, $lm_start, $lm_soluong)
	{
	   //echo '<br/>'.$iduser.'----'.$idcat.'----'.$lm_start.'----'.$lm_soluong.'----<br/>';
	   
	   if($idcat!=0)
       {
            
            mysql_query("set names'utf8'");
    		return $this->database()->select('cauhoi_id, hdch.user_id, title, time_stamp, time_update, privacy, post_status, total_comment, total_view, total_like, hdch.category_id, category_name, us.full_name')
    		->from('phpfox_hoidap_cauhoi hdch, phpfox_hoidap_category hdcat, phpfox_user us')
    		->where('hdch.category_id = hdcat.category_id and post_status = 1 and hdch.user_id = us.user_id and hdch.user_id = '. $iduser .' and hdch.category_id = '. $idcat )
    		->order('hdch.time_update desc') 
    		->limit($lm_start.','.$lm_soluong) 
    		->execute('getSlaveRows');
        }
		
        else
        {
           mysql_query("set names'utf8'");
    		return $this->database()->select('cauhoi_id, hdch.user_id, title, time_stamp, time_update, privacy, post_status, total_comment, total_view, total_like, hdch.category_id, category_name, us.full_name')
    		->from('phpfox_hoidap_cauhoi hdch, phpfox_hoidap_category hdcat, phpfox_user us')
    		->where('hdch.category_id = hdcat.category_id and post_status = 1 and hdch.user_id = us.user_id and hdch.user_id = '. $iduser)
    		->order('hdch.time_update desc') 
    		->limit($lm_start.','.$lm_soluong) 
    		->execute('getSlaveRows');
            
        }
        
	}
	

	
	public function getMyPost($iduser, $idcat, $lm_start, $lm_soluong)
	{
	   //echo '<br/>'.$iduser.'----'.$idcat.'----'.$lm_start.'----'.$lm_soluong.'----<br/>';
	   if($idcat!=0)
       {
			//Khi xem cau hoi o category cha
			if($this->showCauhoiByParentCat($idcat) != 0)
			{
				$child_cat = $this->showCauhoiByParentCat($idcat);
				$str_child_cat = "";
				for($i=0; $i<count($child_cat); $i++)
				{
					if($i == 0)
						$str_child_cat .= 'hdch.category_id = '.$child_cat[$i]['category_id'];
					else
						$str_child_cat .= ' or hdch.category_id = '.$child_cat[$i]['category_id'];
				}

				mysql_query("set names'utf8'");
				return $this->database()->select('cauhoi_id, hdch.user_id, title, time_stamp, time_update, privacy, post_status, total_comment, total_view, total_like, hdch.category_id, category_name, us.full_name')
				->from('phpfox_hoidap_cauhoi hdch, phpfox_hoidap_category hdcat, phpfox_user us')
				->where('hdch.category_id = hdcat.category_id and post_status = 1 and hdch.user_id = us.user_id and hdch.user_id = '. $iduser . ' and (' . $str_child_cat . ')')
				->order('hdch.time_update desc') 
				->limit($lm_start.','.$lm_soluong) 
				->execute('getSlaveRows');
			}
			
			//Xem theo category id con
			else
			{
				mysql_query("set names'utf8'");
				return $this->database()->select('cauhoi_id, hdch.user_id, title, time_stamp, time_update, privacy, post_status, total_comment, total_view, total_like, hdch.category_id, category_name, us.full_name')
				->from('phpfox_hoidap_cauhoi hdch, phpfox_hoidap_category hdcat, phpfox_user us')
				->where('hdch.category_id = hdcat.category_id and post_status = 1 and hdch.user_id = us.user_id and hdch.user_id = '. $iduser .' and hdch.category_id = '. $idcat )
				->order('hdch.time_update desc') 
				->limit($lm_start.','.$lm_soluong) 
				->execute('getSlaveRows');
			
			}
 
        }
		//Xem tat ca cau hoi theo iduser
		else
		{		
           mysql_query("set names'utf8'");
    		return $this->database()->select('cauhoi_id, hdch.user_id, title, time_stamp, time_update, privacy, post_status, total_comment, total_view, total_like, hdch.category_id, category_name, us.full_name')
    		->from('phpfox_hoidap_cauhoi hdch, phpfox_hoidap_category hdcat, phpfox_user us')
    		->where('hdch.category_id = hdcat.category_id and post_status = 1 and hdch.user_id = us.user_id and hdch.user_id = '. $iduser)
    		->order('hdch.time_update desc') 
    		->limit($lm_start.','.$lm_soluong) 
    		->execute('getSlaveRows');
            
        } 
	}
	
	public function showCauhoiByParentCat($idCat)
	{
		mysql_query("set names'utf8'");
    		$data = $this->database()->select('category_parent')
    		->from('phpfox_hoidap_category')
    		->where('category_status = 1 and category_id = '.$idCat)
    		->execute('getSlaveRows');
			
			$cat_parent = $data[0]['category_parent'];
			
			if($cat_parent == 0)
			{
				$data = $this->database()->select('category_id')
				->from('phpfox_hoidap_category')
				->where('category_status = 1 and category_parent = '.$idCat)
				->execute('getSlaveRows');
				return $data;
			}
			return 0;
			
	}
    
    public function getCategory_bk()
    {
        mysql_query("set names'utf8'");
    		return $this->database()->select('category_id, category_name')
    		->from('phpfox_hoidap_category')
    		->where('category_status = 1')
    		->order('category_id') 
    		//->limit('0,10') 
    		->execute('getSlaveRows');
    }
    
    public function soLuongCauHoi($iduser, $idcat)
    {
		if($idcat!=0)
		{
			//Khi xem cau hoi o category cha
			if($this->showCauhoiByParentCat($idcat) != 0)
			{
				$child_cat = $this->showCauhoiByParentCat($idcat);
				$str_child_cat = "";
				for($i=0; $i<count($child_cat); $i++)
				{
					if($i == 0)
						$str_child_cat .= 'category_id = '.$child_cat[$i]['category_id'];
					else
						$str_child_cat .= ' or category_id = '.$child_cat[$i]['category_id'];
				}
				
				mysql_query("set names'utf8'");
				$count = $this->database()->select('count(*) as sum')
				->from('phpfox_hoidap_cauhoi')
				->where('post_status = 1 and user_id = '. $iduser .' and (' . $str_child_cat . ')')
				->execute('getSlaveRows');
			}		
			//Xem theo category id con
			else
			{
				 mysql_query("set names'utf8'");
				$count = $this->database()->select('count(*) as sum')
				->from('phpfox_hoidap_cauhoi')
				->where('post_status = 1 and user_id = '. $iduser .' and category_id = '. $idcat)
				->execute('getSlaveRows');
			
			}
        }
		//Xem tat ca cau hoi theo iduser
		else
		{		
			mysql_query("set names'utf8'");
    		$count = $this->database()->select('count(*) as sum')
    		->from('phpfox_hoidap_cauhoi')
    		->where('post_status = 1 and user_id = '. $iduser )
    		->execute('getSlaveRows');
        }
		return $count[0]['sum']; 
    }
    
    public function getCategory()
    {
        
        mysql_query("set names'utf8'");
		$cat = $this->database()->select('category_id, category_name, category_image')
		->from('phpfox_hoidap_category')
		->where('category_status = 1 and category_parent = 0 ')
        ->order('category_order')
		->execute('getSlaveRows');
        
        for($i=0; $i<count($cat); $i++)
        {
            if($i%4==0)
                $cat[$i]['class_firsr'] = "first_cat";
            else
                 $cat[$i]['class_firsr'] = "";
                 
            $idCat_Parent = $cat[$i]['category_id'];
            $child_cat = $this->database()->select('category_id, category_name, category_image')
    		->from('phpfox_hoidap_category')
    		->where('category_status = 1 and category_parent = '. $idCat_Parent)
            ->order('category_order')
    		->execute('getSlaveRows');
            $cat[$i]['child_cat'] = $child_cat;
            //print_r($child_cat);
        }
        return $cat;
    }
    
    public function getNameCategory($idCat)
    {
        mysql_query("set names'utf8'");
		$nameCat = $this->database()->select('category_id, category_name, category_image')
		->from('phpfox_hoidap_category')
		->where('category_id = '. $idCat)
		->execute('getSlaveRows');
        return $nameCat[0]['category_name'];
    }
    
	function bodau($str)
	{
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
		$str = preg_replace("/(đ)/", 'd', $str);

		$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
		$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
		$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
		$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
		$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
		$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
		$str = preg_replace("/(Đ)/", 'D', $str);  
		//$str = preg_replace("/(\@|\?|\,|\.|\:|\;|\"|\%|\/|\\|\?|\<|\>|\#|\^|\||\=|\!|\*|\&|\_|\(|\))/", "_", $str);
		
		$value = str_replace(" ","-",$str);
		return $value;
	}
    
    
    
    
    
    
    
    
}

?>